package com.open.party.cloud.view.home.jiCengDangJian.org.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.open.party.cloud.R;
import com.sinothk.android.utils.XUtils;

import java.util.ArrayList;
import java.util.List;

import cn.sinothk.view.treeView.TreeListViewAdapter;
import cn.sinothk.view.treeView.bean.Node;

import static android.content.Context.LAYOUT_INFLATER_SERVICE;

/**
 * Created by Sogrey on 2018/8/9.
 */

public class OrgTreeViewAdapter extends TreeListViewAdapter {

//    int checkbox_checked, checkbox_uncheck;

    public OrgTreeViewAdapter(ListView mTree, Context context, List<Node> datas, int defaultExpandLevel) {
        super(mTree, context, datas, defaultExpandLevel, R.drawable.ic_collapsing_999, R.drawable.ic_expanding_999);
//
//        this.checkbox_checked = checkbox_checked;
//        this.checkbox_uncheck = checkbox_uncheck;
    }

    public OrgTreeViewAdapter(ListView mTree, Context context, List<Node> datas, int defaultExpandLevel, int checkbox_checked, int checkbox_uncheck) {
        super(mTree, context, datas, defaultExpandLevel, R.drawable.ic_collapsing_999, R.drawable.ic_expanding_999);
//
//        this.checkbox_checked = checkbox_checked;
//        this.checkbox_uncheck = checkbox_uncheck;
    }

    @Override
    protected void onLeafClickListener(int position, Node node) {
        if (mOnItemCheckedListener != null) {
            mOnItemCheckedListener.onClick(position, node);
        }
    }

    @Override
    public View getConvertView(final Node node, final int position, View convertView, ViewGroup parent) {
        View view;
        final ViewHolder holder;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.org_tree_view_item, null);

            holder = new ViewHolder();
            holder.rootView = view.findViewById(R.id.rootView);
            holder.treeViewTitleTv = view.findViewById(R.id.treeViewTitleTv);
            holder.iv_tree_icon = view.findViewById(R.id.iv_tree_icon);
            holder.moreIcon = view.findViewById(R.id.moreIcon);
            holder.numTipTv = view.findViewById(R.id.numTipTv);

            view.setTag(holder);

        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }

        if (node.isLeaf()) {
            String id = node.getId().toString();
            if (XUtils.string().isNotEmpty(id)) {
                holder.treeViewTitleTv.setTextColor(ContextCompat.getColor(mContext, R.color.white));

                holder.rootView.setBackgroundResource(R.drawable.shape_bg_party_square_round_leaf);
                holder.iv_tree_icon.setImageResource(R.drawable.icon_zhibu);
                holder.moreIcon.setImageResource(R.drawable.icon_sanxing2);
            }else{
                holder.treeViewTitleTv.setTextColor(ContextCompat.getColor(mContext, R.color.page_title));
                holder.rootView.setBackgroundResource(R.drawable.shape_bg_party_square_round_user);
                holder.iv_tree_icon.setImageResource(R.mipmap.tab_mine);
                holder.moreIcon.setImageResource(R.drawable.transparent_bg);
            }

            holder.numTipTv.setVisibility(View.INVISIBLE);
        } else {
            holder.iv_tree_icon.setImageResource(R.drawable.icon_org_logo);
            holder.treeViewTitleTv.setTextColor(ContextCompat.getColor(mContext, R.color.white));
            holder.rootView.setBackgroundResource(R.drawable.shape_bg_party_square_round);

            if (node.isExpand()) {
                holder.moreIcon.setImageResource(R.drawable.more_white_top);
            } else {
                holder.moreIcon.setImageResource(R.drawable.more_white_bottom);
            }

            if (node.getChildren() != null && node.getChildren().size() > 0) {
                holder.numTipTv.setText("( " + node.getChildren().size() + " )");
                holder.numTipTv.setVisibility(View.VISIBLE);
            } else {
                holder.numTipTv.setVisibility(View.INVISIBLE);
                holder.rootView.setBackgroundResource(R.drawable.shape_bg_party_square_round_sub_item);
            }

            if (node.getParent() != null) {
                holder.rootView.setBackgroundResource(R.drawable.shape_bg_party_square_round_sub_item);
            }
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                expandOrCollapse(position);
            }
        });

        if (exPandositions.contains(node.getId() + "")) {
            expand(position);
            exPandositions.remove(node.getId() + "");
        }

        holder.treeViewTitleTv.setText(node.getName());
        return view;
    }

    List<String> exPandositions = new ArrayList<>();

    public void expandIds(List<String> positions) {
        exPandositions.addAll(positions);
        notifyDataSetChanged();
//        exPandositions.clear();
    }

    class ViewHolder {
        RelativeLayout rootView;
        ImageView iv_tree_icon, moreIcon;
        TextView treeViewTitleTv, numTipTv;
    }

    private OnItemClickListener mOnItemCheckedListener;

    public void setOnItemListener(OnItemClickListener mOnItemCheckedListener) {
        this.mOnItemCheckedListener = mOnItemCheckedListener;
    }

    public interface OnItemClickListener {
        //        /**
//         * @param seletcedNodes 选中的节点数据
//         * @param position      位置索引
//         * @param node          item数据
//         * @param isCheck       是否选中 true 选中，false 取消选中
//         */
//        void post(ArrayList<TreeItemBean> seletcedNodes, int position, Node node, boolean isCheck);
        void onClick(int position, Node node);

    }
}
